package com.supermap.gwfs.message.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.gwfs.message.common.ConfigReader;

/**
 * 风速风向转编码
 */
public class WindCodeCalculator {

    public WindCodeCalculator(){

    }

    /**
     * 风向转编码
     * @param direction 风向
     * @return
     */
    public int calcDirectionCode(double direction) {
        int code = 1;
        JSONArray windDirectionConfig = ConfigReader.getInstance().getWindDirectionCode();
        for (int i = 0; i < windDirectionConfig.size(); i++) {
            JSONObject directionConfig = windDirectionConfig.getJSONObject(i);
            if(direction >= directionConfig.getDouble("minValue") && direction < directionConfig.getDouble("minValue")){
                return directionConfig.getInteger("code");
            }
        }
        return code;
    }

    /**
     * 风速转编码
     * @param speed 风速
     * @return
     */
    public int calcSpeedCode(double speed) {
        int code = 0;
        JSONArray windSpeedConfig = ConfigReader.getInstance().getWindSpeedCode();
        for (int i = 0; i < windSpeedConfig.size(); i++) {
            JSONObject speedCode = windSpeedConfig.getJSONObject(i);
            if(speed > speedCode.getDouble("minValue") && speed <= speedCode.getDouble("minValue")){
                return speedCode.getInteger("code");
            }
        }
        return code;
    }
}
